from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
from lib.common import AbstractHandler
import logging
app_logger = logging.getLogger("student")
from django.core.cache import cache
from services.datamodel.student.models import mm_StudentAgent, mm_StudentCapital, mm_Student
from services.datamodel.order.models import mm_Withdrawal
from urllib.parse import unquote
from urllib import parse
import json


class Handler(AbstractHandler):

    def __init__(self):
        AbstractHandler.__init__(self, app_logger)

    @csrf_exempt
    def handle_contract_notify(self, request):
        app_logger.error("这是公猫电签的回调")
        app_logger.error(request.body)
        text = unquote(request.body.decode(), 'utf-8')
        notify_data = dict(parse.parse_qsl(text))
        app_logger.error(notify_data)
        res = mm_StudentAgent.get_or_default(student_id=notify_data['extraParam'])
        if not res:
            user = mm_Student.get_or_default(mobile=notify_data['mobile'])
            if not user:
                app_logger.error("这是电签回调失败,查询手机号也失败")
                return JsonResponse({"success": False})
            res = mm_StudentAgent.get_or_default(student_id=user.id)
            if not res:
                return JsonResponse({"success": True})
        if notify_data['status'] == "1":
            app_logger.error("这是电签回调失败")
            return JsonResponse({"success": True})
        res.username = notify_data['name']
        res.mobile = notify_data['mobile']
        res.collection_name = notify_data['name']
        res.id_cards = notify_data['identity']
        res.gongmall_status = int(notify_data['status'])
        res.bank_code = notify_data['salaryAccount']
        res.bank_of_deposit = notify_data['bankName']
        res.save()
        return JsonResponse({"success": True})

    @csrf_exempt
    def handle_withdraw_notify(self, request):
        app_logger.error("这是公猫提现的回调")
        app_logger.error(request.body)
        text = unquote(request.body.decode(), 'utf-8')
        notify_data = dict(parse.parse_qsl(text))
        app_logger.error(notify_data)
        res = mm_Withdrawal.get_or_default(request_id=notify_data['requestId'])
        if not res:
            app_logger.error("这是提现回调未找到数据")
            return JsonResponse({"success": False})
        if notify_data['status'] == "2":
            app_logger.error("这是提现回调失败")
            res.status = 1
            res.operation_status = 5
            res.remark = "这是工猫返回的提示:"+notify_data['failReason']
            res.save()
            return JsonResponse({"success": True})
        res.name = notify_data['name']
        res.mobile = notify_data['mobile']
        res.currentRealWage = notify_data['currentRealWage']
        res.taxCalculationType = notify_data['taxCalculationType']
        res.currentTaxAmountOfPerson = notify_data['currentTaxAmountOfPerson']
        res.currentTaxAmountOfCompany = notify_data['currentTaxAmountOfCompany']
        res.identity = notify_data['identity']
        res.status = 2
        res.operation_status = 3
        res.payTime = notify_data['payTime']
        res.save()
        # 更新提现佣金
        ids = json.loads(res.order_ids)
        mm_StudentCapital.filter(order_id__in=ids).update(status=4)
        return JsonResponse({"success": True})

gongmall_handler = Handler()